import { getCurrentDate } from './infra/common/utils';

class ENV {
  ENVIRONMENT = process.env.NODE_ENV || 'development';
  PORT = process.env.PORT || 3000;
  ENV_DIR = `./env/.env.${this.ENVIRONMENT}`;
  VERSION = '0.0.1';

  // swagger
  SWAGGER_TITLE = 'nest bedrock';
  SWAGGER_DESCRIPTION = 'The service start date is ' + getCurrentDate();
  SWAGGER_DOCS = 'docs';

  // databse
  DB_HOST = 'localhost';
  DB_PORT = 3306;
  DB_USER = 'root';
  DB_PASSWORD = 'root';
  DB_DATABASE = 'nest';

  // jwt
  JWT_SECRET = 'secret';
  JWT_EXPIRES_IN = '1d';

  // salt round
  SALT_ROUND = 10;

  // image upload
  UPLOAD_DIR = 'uploads';
  UPLOAD_IMG_MAX_FILE_SIZE = 1024 * 1024 * 2; // 2MB
  UPLOAD_IMG_EXT = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];
  UPLOAD_FILE_EXT = ['pdf', 'doc', 'docx', 'xls', 'xlsx'];
  UPLOAD_FILE_MAP = {
    pdf: 'application/pdf',
    doc: 'application/msword',
    docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    xls: 'application/vnd.ms-excel',
    xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  };
  UPLOAD_VIDEO_EXT: string[] = ['mp4', 'mov', 'avi'];
  UPLOAD_VIDEO_MAX_FILE_SIZE: number = 1024 * 1024 * 500; // 500MB

  // wx
  WX_URL = 'https://api.weixin.qq.com';
  WX_SESSION_ENDPOINT = 'sns/jscode2session';
  WX_APP_ID = 'wxb209417f272ce980';
  WX_SECRET = 'fce44737ba3a393a9e6d4d313b70ec81';
  WX_GRANT_TYPE = 'authorization_code';

  private static instance: ENV;

  constructor() {
    this.loadEnv();
  }

  static getInstance() {
    if (!this.instance) {
      this.instance = new ENV();
    }
    return this.instance;
  }

  public loadEnv() {
    console.log(`load env ${this.ENV_DIR}`);
    require('dotenv').config({ path: this.ENV_DIR });

    this.DB_HOST = process.env.DB_HOST || 'localhost';
    this.DB_PORT = Number(process.env.DB_PORT) || 3306;
    this.DB_USER = process.env.DB_USER || 'root';
    this.DB_PASSWORD = process.env.DB_PASSWORD || 'root';
    this.DB_DATABASE = process.env.DB_DATABASE || 'nest';

    this.JWT_SECRET = process.env.JWT_SECRET || 'secret';
    this.JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN || '1d';
    this.SALT_ROUND = Number(process.env.SALT_ROUND) || 10;
  }

  get isDevelopment() {
    return this.ENVIRONMENT === 'development';
  }

  get isProduction() {
    return this.ENVIRONMENT === 'production';
  }

  get isTest() {
    return this.ENVIRONMENT === 'test';
  }
}

export const env = ENV.getInstance();
